package com.jcp.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jcp.entity.Files;
import com.jcp.entity.FilesMin;
import com.jcp.entity.Notices;
import com.jcp.service.FilesService;
import com.jcp.service.NoticesService;
import com.jcp.serviceimp.FilesServiceImp;
import com.jcp.serviceimp.NoticesServiceImp;
import com.jcp.util.DBUtil;
import com.jcp.util.PageData;

/**
 * Servlet implementation class NoticesServlet
 */
@WebServlet("/ns.do")
public class NoticesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	NoticesService ns = new NoticesServiceImp();//创建公告服务层的对象
    FilesService fs = new FilesServiceImp(); 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public NoticesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置请求编码
		request.setCharacterEncoding("utf-8");
		//设置响应编码
		response.setCharacterEncoding("utf-8");
			//创建一个op的字符串进行判断功能选择
			String op = "";
			//判断request.getParameter("op")是否有值：有值则对op进行赋值
			if(request.getParameter("op1")!=null) {
				//op赋值
				op = request.getParameter("op1");
			}
			//登入操作
			if(op.equals("login")) {
				//对Notices数据库进行全查询
				selQuery(request, response);
			}
			//公告管理的添加功能
			if(op.equals("add")) {
				//获取add表单中的公告内容
				String notContext = request.getParameter("username1");
				//获取add表单中的公告时间
				String notRepass = request.getParameter("repass1");
				//将数据封装到Notices中
				Notices notices = new Notices(notContext, 0, notRepass);
				
				boolean flag = ns.addNotices(notices);
				
			}
				//公告管理的添加功能由部长发布
			else  if(op.equals("add1")) {
					//获取add表单中的公告标题
					String title = request.getParameter("title");
					//获取add表单中的公告内容
					String desc = request.getParameter("desc");
					String  notContext=title+desc ;
					int  userId=0;
					//还需将当前sessionon储存的对象的posId
					if(request.getParameter("userId")!=null) {
						
						userId = Integer.parseInt(request.getParameter("userId"));
					}
					//将数据封装到Notices中
					Notices notices = new Notices(notContext,userId , null);
					
					boolean flag = ns.addNotices(notices);
					//PrintWriter out =response.getWriter();
					//out.print(out);
					//out.close();
					request.getRequestDispatcher("min.do").forward(request, response);
					
			}else if(op.equals("select")) {//公告管理的查询功能
				//创建一个获取表单参数数据变量
				String notContent = "";
				//判断表单数据是否为空值
				if(request.getParameter("username2")!=null) {
					
					notContent = request.getParameter("username2");
				}
				
				List<Notices> list = ns.selNotices(notContent,1);
				//存储list<Notices>的属性	
				request.setAttribute("notices", list);
				//Notices_1.js跳转
				request.getRequestDispatcher("Notices_1.jsp").forward(request, response);
			}else if(op.equals("filesadd")) {
				//获得 files_1添加档案页面的text里面的数据
//				String userName=request.getParameter("userName");
				String nation= request.getParameter("nation");
				String address=request.getParameter("address");
				String cardId=request.getParameter("cardId");
				String sex=request.getParameter("sex");
				String birthday=request.getParameter("birthday");
				String political=request.getParameter("political");
				String isMarried=request.getParameter("isMarried");
				String school=request.getParameter("school");
				String graduateTime=request.getParameter("graduateTime");
				String major=request.getParameter("major");
				String phone=request.getParameter("phone");
				String blood=request.getParameter("blood");
				double weight=Double.parseDouble(request.getParameter("weight"));
				double height=Double.parseDouble(request.getParameter("height"));
				String education=request.getParameter("education");
				String cv1=request.getParameter("cv1");
				String cv2=request.getParameter("cv2");
				String cv3=request.getParameter("cv3");
				String photosurl = request.getParameter("photosurl");
				
				Files files = new Files(sex,nation,address,birthday,cardId,political,isMarried,school,graduateTime,major,cv1,
						cv2,cv3,phone,blood,weight,height,education,photosurl);
				
				boolean flag= fs.addFiles(files);
				
				if(flag) {
					System.out.println("操作成功");
				}else {
					System.out.println("操作失败");
				}
				
		}else if(op.equals("filesminsel")) {
			
			int page=1;
			int pageSize=10;
			
			if(request.getParameter("page")!=null) {
				page=Integer.valueOf(request.getParameter("page"));
			}
			if(request.getParameter("pageSize")!=null) {
				pageSize=Integer.valueOf(request.getParameter("pageSize"));
			}
			PageData<FilesMin> pageData = fs.getselFilesminMinPage(page, pageSize);
			System.out.println("pageData"+pageData+"pageSize "+pageSize);
			request.setAttribute("pageData",pageData);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
//			String notContent = "";
//			//判断表单数据是否为空值
//			if(request.getParameter("username2")!=null) {
//				
//				notContent = request.getParameter("username2");
//			}
//			
//			List<Notices> list = ns.selNotices(notContent,1);
//			//存储list<Notices>的属性	
//			request.setAttribute("notices", list);
//			//Notices_1.js跳转
//			request.getRequestDispatcher("Notices_1.jsp").forward(request, response);
			
			
			
		}else if(op.equals("filesminsel1")) {
			
			int page=1;
			int pageSize=10;
			String strLike = "";
			if(request.getParameter("page")!=null) {
				page=Integer.valueOf(request.getParameter("page"));
			}
			if(request.getParameter("pageSize")!=null) {
				pageSize=Integer.valueOf(request.getParameter("pageSize"));
			}
			if (request.getParameter("strLike") != null) {
				strLike = request.getParameter("strLike");
			}
			PageData<FilesMin> pageData = fs.getselFilesminMinPage(page, pageSize);
//			System.out.println("pageData"+pageData+"pageSize "+pageSize);
			request.setAttribute("pageData",pageData);
			request.setAttribute("strLike",strLike);
			request.getRequestDispatcher("files_2.jsp").forward(request, response);
		}
	}
	/**
	 * 公告管理系统通用查询
	 * @param request：请求编码
	 * @param response：响应编码
	 * @throws UnsupportedEncodingException
	 * @throws ServletException
	 * @throws IOException
	 */
	public void selQuery(HttpServletRequest request, HttpServletResponse response)
			throws UnsupportedEncodingException, ServletException, IOException {
		//模糊查询的关键字段
		String notContent = "";
		
		//对公告表进行全查询
		List<Notices> list = ns.selNotices(notContent,1);
		//存储list<Notices>的属性
		request.setAttribute("notices", list);
		
		//Notices_1.js的跳转
		request.getRequestDispatcher("Notices_1.jsp").forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
